package com.csgc.camera.application

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Process
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import com.amap.api.location.AMapLocationClient
import com.amap.apis.utils.core.api.AMapUtilCoreApi


import com.csgc.camera.storage.CommonPreference
import com.csgc.camera.storage.base.BasePreferences
import com.csgc.camera.util.AppUtil
import com.csgc.camera.util.SpUtils
import com.csgz.cleanmaster.common.AppLifecycleTracker
import com.cszy.mfjx.lib_hlyj_base.BuildConfig

import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.hjq.toast.ToastUtils
import com.hlyj.http.base.tool.lib_hlyj_base.BaseApplication
import com.hlyj.http.base.tool.lib_hlyj_base.Constants
import com.hlyj.http.base.tool.lib_hlyj_base.listener.IChannelProvider
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.OverlayViewUtils
import com.tencent.mmkv.MMKV
import me.jessyan.autosize.AutoSizeConfig
import kotlin.properties.Delegates

class CameraApplication: BaseApplication(), Application.ActivityLifecycleCallbacks,
    IChannelProvider {
    override fun onCreate() {
        super.onCreate()
        AutoSizeConfig.getInstance().isExcludeFontScale = true
        ToastUtils.init(instance)
        BasePreferences.init(instance)
        XLog.init( if (BuildConfig.DEBUG) LogLevel.ALL else LogLevel.INFO)
        registerActivityLifecycleCallbacks(this)
        MMKV.initialize(this)
        var session = BaseModuleSpUtils.getInstance().getInt(Constants.SESSIONID,0);
        session+=1;
        BaseModuleSpUtils.getInstance().putInt(Constants.SESSIONID,session);
        restartSplashListener()
        AMapUtilCoreApi.setCollectInfoEnable(false)
//        if (SpUtils.getInstance().getBoolean(BaseAppConstans.isAgreeMent)){
//            AMapUtilCoreApi.setCollectInfoEnable(false)
//            AMapLocationClient.updatePrivacyShow(this,true,true)
//            AMapLocationClient.updatePrivacyAgree(this,true)
//        }
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        instance = this
        MultiDex.install(this);

    }

    override fun onActivityCreated(p0: Activity, p1: Bundle?) {

    }

    override fun onActivityStarted(p0: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {
        val rootView = activity.window.decorView.rootView
        rootView.viewTreeObserver.addOnWindowFocusChangeListener { hasFocus ->
            if (hasFocus && !activity.isFinishing && !activity.isDestroyed) {
                // 如果 Activity 不属于目标包 且 不是 HtmlActivity，才显示弹窗
                if (!OverlayViewUtils.isTargetPackageActivity(activity)) {
                    var privacyId= BaseModuleSpUtils.getInstance().getString(Constants.PRIVACYID)
                    //  Log.e("onActivityResumed", "onActivityResumed: $privacyId")
                    //OverlayViewUtils.removeCustomOverlay()
                    OverlayViewUtils.showCustomOverlay(activity, privacyId)
                }
            }
        }
    }

    override fun onActivityPaused(p0: Activity) {

    }

    override fun onActivityStopped(p0: Activity) {

    }

    override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {

    }

    override fun onActivityDestroyed(p0: Activity) {

    }

    override fun getChannelId(): String {
        return AppUtil.getChannel()
    }

    private fun  restartSplashListener() {
        // 注册生命周期监听
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleTracker)
    }
}